/**
 * Created by JasonChou on 16/6/6.
 */
import user from '../api/user'
import * as types from './mutationTypes'


export const login = ({ dispatch }) => {
};

export const register = ({ dispatch ,state}) => {

  var succ = (userInfo) => dispatch(types.REGISTER_SUCCESS, userInfo);
  var fail = (msg) => dispatch(types.REGISTER_FAILURE);


  user.register(state.register).then(
    (obj)=> {
      if (obj.code === 0)
        succ(obj.user);
      else
        fail(obj.msg);
    }).catch(()=>fail());
};

export const autoLogin = ({ dispatch}) => {

  var succ = ({userInfo,setting}) => dispatch(types.LOGIN_SUCCESS, userInfo, setting);
  var fail = (msg) => dispatch(types.LOGIN_FAILURE);

  return user.autoLogin().then(
    (obj)=> {
      if (obj.code === 0)
        succ(obj.data);
      else
        fail(obj.msg);

      return obj;
    }).catch(()=>fail());
};
